<%@page import="com.carnation.utils.MenuFileUtril"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.carnation.utils.SessionInfo" %>	
<%@ include file="../taglibs.jsp"%>
<%@ include file="../easyuiLib.jsp"%>
<% SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute("sessionInfo"); 
	MenuFileUtril menuFileUtril = new MenuFileUtril();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" charset="utf-8">

	function uploadfile() {
		$('#uploadForm').form('submit', {
			url : '${ctx}/upload',
			onSubmit : function() {
				var flag = checkFile();
				return $('#uploadForm').form('validate') && flag;
			},
			success : function(data) {
				console.log(data);
				var json = JSON.parse(data);
				$.messager.alert('结果',json.msg);
				if(json.status == "200"){
					window.parent.filesearch();
					clearDRF();
				}
			}
		});
	}
	
	function clearDRF(){
		$('#d').textbox('clear');
		$('#r').textbox('clear');
		var obj = document.getElementById('ft'); 
		obj.outerHTML=obj.outerHTML;
	}
	
	function checkFile(){
		var f = document.getElementById("ft").files;
		console.log(f[0]);
		if(f && f[0].size < <%=menuFileUtril.getLimitSize()%>){
			var filename = f[0].name;
			console.log(filename);
			var filetype = '<%=menuFileUtril.getFileType()%>';
			var filetypes = filetype.split(";");
			console.log(filetypes);
			if(filetypes == null){
				return true;
			}
			if(filename.lastIndexOf(".") != -1){
				var type = filename.substr(filename.lastIndexOf(".")+1);
				for(var i=0;i<filetypes.length;i++){
					if(filetypes[i] == "*" || filetypes[i] == type){
						console.log(filetypes[i]);
						return true;
					}
				}
				$.messager.alert("提示","格式不符合");
				return false;
			}
			else{
				$.messager.aler("提示","不能上传未知类型文件");
				return false;
			}
		}
		else{
			$.messager.alert('提示','上传文件的大小超过设定值<%=menuFileUtril.getLimitSizeM()%>M');
			return false;
		}
	}
</script>
</head>
<body>
	<form id="uploadForm" class="easyui-form" enctype="multipart/form-data" method="post">
		<table>
			<tr style="display:none;">
				<td>catalogId</td>
				<td><input type="text" name="catalogid" value="<%=request.getParameter("id")%>" ></td>
			</tr>
			<tr>
				<td><span>介质描述:</span></td>
				<td><input name="description" class="easyui-textbox" id="d"
					data-options="multiline:true,required:true"
					style="width: 300; height: 150;" /></td>
			</tr>
			<tr>
				<td><span>备注：</span></td>
				<td><input name="remark" class="easyui-textbox" id="r"
					data-options="multiline:true" style="width: 300; height: 150" /></td>
			</tr>
			<tr>
				<td><span>上文件：</span></td>
				<td><input id="ft" type="file" class="easyui-tooltip" name="file" style="width: 300;" title="请选择文件,最大上传文件大小为<%=menuFileUtril.getLimitSizeM()%>M,支持的文件格式:<%=menuFileUtril.getFileType()%>"></td>
			</tr>
			<tr>
				<td></td>
				<td><span style="float: right"><a
						class="easyui-linkbutton" onclick="uploadfile()">确认上传</a> <a
						class="easyui-linkbutton" onclick="clearDRF()">清空</a></span></td>
			</tr>
		</table>
	</form>
</body>
</html>